package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;


	/**
	 * 判断用户是否购买过指定专辑
	 * @param albumId
	 * @return
	 */
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	@GuiGuLogin
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		Boolean flage = userInfoService.isPaidAlbum(albumId);
		return Result.ok(flage);
	}

	/**
	 * 根据专辑id+用户ID获取用户已购买声音id列表
	 * @param albumId
	 * @return
	 */
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	@GuiGuLogin(require = false)
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		if(userId == null) return Result.ok(Collections.emptyList());
		List<Long> list = userInfoService.findUserPaidTrackList(albumId,userId);
		return Result.ok(list);
	}

	/**
	 * 根据用户id获取用户信息
	 * @param userId
	 * @return
	 */
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfo userInfo = userInfoService.getById(userId);
		UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
		return Result.ok(userInfoVo);
	}

	/**
	 * 该接口提供给给专辑服务，展示声音列表动态判断付费标识
	 * 判断当前用户某一页中声音列表购买情况
	 *
	 * @param userId               用户ID
	 * @param albumId              专辑ID
	 * @param needChackTrackIdList 待检查购买情况声音列表
	 * @return data:{声音ID：购买结果}   结果：1（已购）0（未购买）
	 */
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long,Integer>> userIsPaidTrack(@PathVariable Long userId,@PathVariable Long albumId,@RequestBody List<Long> needChackTrackIdList){
		Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,needChackTrackIdList);
		return Result.ok(map);
	}

	/**
	 * 保存订单记录
	 * @param userPaidRecordVo
	 * @return
	 */
	@PostMapping("/userInfo/savePaidRecord")
	@GuiGuLogin
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}
}






















